package com.googlecode.grima.base;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.Transient;

import com.googlecode.grima.domain.Identifiable;

@MappedSuperclass
public abstract class Entity implements Identifiable<Long>, Serializable {

	private static final long serialVersionUID = 8255856510878723406L;

	private Long id;
	private Date createdOn;
	private String createdBy;
	private Date updatedOn;
	private String updatedBy;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "ID")
	public Long getId() {
	  return this.id;
  }

  public void setId(Long id) {
		this.id = id;
  }
	
	@Column(name = "CREATED_ON")
	public Date getCreatedOn() {
		return createdOn;
	}

	public void setCreatedOn(Date createdOn) {
		this.createdOn = createdOn;
	}

	@Column(name = "CREATED_BY")
	public String getCreatedBy() {
		return createdBy;
	}

	public void setCreatedBy(String createdBy) {
		this.createdBy = createdBy;
	}

	@Column(name = "UPDATED_ON")
	public Date getUpdatedOn() {
		return updatedOn;
	}

	public void setUpdatedOn(Date updatedOn) {
		this.updatedOn = updatedOn;
	}

	@Column(name = "UPDATED_BY")
	public String getUpdatedBy() {
		return updatedBy;
	}

	public void setUpdatedBy(String updatedBy) {
		this.updatedBy = updatedBy;
	}
	
	@Override
	@Transient
	public Long getPrimaryKey() {
		return getId();
	}

	@Override
	public void setPrimaryKey(Long id) {
		setId(id);
	}
	
	@Override
	@Transient
	public boolean isPrimaryKeySet() {
		return getId() != null && getId().longValue() != 0;
	}

	@Override
	public boolean equals(final Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (!(obj instanceof Entity)) {
			return false;
		}
		final Entity other = (Entity) obj;
		if (this.getId() != null && other.getId() != null) {
			if (this.getId() != other.getId()) {
				return false;
			}
		}
		return true;
	}
	
  @Override
  public String toString() {
      StringBuffer strBuff = new StringBuffer();
      strBuff.append("id : ").append(getId());
      return strBuff.toString();
  }


}